package zhoufu.sz.sanxiao_check.ui.train;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.TrainListModel;
import zhoufu.sz.sanxiao_check.presents.TrainListPresenter;
import zhoufu.sz.sanxiao_check.ui.adapter.TrainAdapter;
import zhoufu.sz.sanxiao_check.ui.contract.TrainListContract;
import zhoufu.sz.sanxiao_check.ui.fragment.BaseListFragment;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/8/4
 */
public class TrainListFragment extends BaseListFragment implements TrainListContract.View
{


    @Inject
    TrainListPresenter _presenter;

    public static TrainListFragment newInstance(String SID)
    {
        Bundle args = new Bundle();
        args.putString("SID", SID);
        TrainListFragment fragment = new TrainListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        LoginComponet componet = getComponent(LoginComponet.class);
        componet.inject(this);
        _presenter.bindView(this);
    }

    @Override protected void initAdapter()
    {
        mAdapter = new TrainAdapter(mAct, null, R.layout.item_train_list);
    }

    @Override public void onStart()
    {
        super.onStart();
    }

    @Override public void onResume()
    {
        super.onResume();
        _presenter.onStart();
    }

    @Override public String SID()
    {
        return getArguments().getString("SID");
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout)
    {
        _presenter.setPageFirst();
        mAdapter.clear();
        _presenter.onStart();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout)
    {
        if ( mAdapter.getItemCount() < _presenter.getTotalSize() )
        {
            _presenter.next();
            return true;
        } else
        {
            return false;
        }
    }

    @Override public void onDestroy()
    {
        super.onDestroy();
        _presenter.onDestroy();
    }

    @Override public void onItemClick(View itemView, int viewType, int position)
    {
        super.onItemClick(itemView, viewType, position);
        TrainListModel model = (TrainListModel) mAdapter.getList().get(position);
        mAct._navigator.startAddTrainAct(mAct, model);
    }
}
